package com.micro.ai.files.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.micro.ai.commons.domain.ApiResponse;
import com.micro.ai.files.entity.FileVersion;
import com.micro.ai.files.service.FileVersionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 文件版本控制器
 * 
 * @author micro-ai
 * @since 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/api/file-versions")
@Tag(name = "文件版本管理", description = "文件版本的创建、查询、恢复等功能")
public class FileVersionController {

    @Autowired
    private FileVersionService fileVersionService;

    @PostMapping
    @PreAuthorize("hasAuthority('file_version:create')")
    @Operation(summary = "创建文件版本", description = "创建新的文件版本")
    public ApiResponse<FileVersion> create(@Valid @RequestBody FileVersion version) {
        FileVersion result = fileVersionService.create(version);
        return ApiResponse.success("创建成功", result);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('file_version:delete')")
    @Operation(summary = "删除文件版本", description = "删除文件版本")
    public ApiResponse<Void> delete(
            @Parameter(description = "版本ID", required = true) @PathVariable String id) {
        fileVersionService.delete(id);
        return ApiResponse.successVoid("删除成功");
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('file_version:view')")
    @Operation(summary = "获取版本详情", description = "根据ID获取版本详细信息")
    public ApiResponse<FileVersion> getById(
            @Parameter(description = "版本ID", required = true) @PathVariable String id) {
        FileVersion version = fileVersionService.getById(id);
        if (version == null) {
            return ApiResponse.error("F0002", "文件版本不存在");
        }
        return ApiResponse.success(version);
    }

    @GetMapping("/page")
    @PreAuthorize("hasAuthority('file_version:view')")
    @Operation(summary = "分页查询文件版本", description = "分页查询文件版本列表")
    public ApiResponse<Page<FileVersion>> pageQuery(
            @Parameter(description = "页码", example = "1") @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @Parameter(description = "每页数量", example = "20") @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @Parameter(description = "文件ID", required = true) @RequestParam("fileId") String fileId) {
        Page<FileVersion> page = fileVersionService.pageQuery(pageNum, pageSize, fileId);
        return ApiResponse.success(page);
    }

    @GetMapping("/by-file/{fileId}")
    @PreAuthorize("hasAuthority('file_version:view')")
    @Operation(summary = "查询文件的所有版本", description = "根据文件ID查询所有版本")
    public ApiResponse<List<FileVersion>> listByFileId(
            @Parameter(description = "文件ID", required = true) @PathVariable String fileId) {
        List<FileVersion> list = fileVersionService.listByFileId(fileId);
        return ApiResponse.success(list);
    }

    @GetMapping("/latest/{fileId}")
    @PreAuthorize("hasAuthority('file_version:view')")
    @Operation(summary = "获取最新版本", description = "获取文件的最新版本")
    public ApiResponse<FileVersion> getLatestVersion(
            @Parameter(description = "文件ID", required = true) @PathVariable String fileId) {
        FileVersion version = fileVersionService.getLatestVersion(fileId);
        if (version == null) {
            return ApiResponse.error("F0002", "未找到文件版本");
        }
        return ApiResponse.success(version);
    }

    @PutMapping("/{id}/restore")
    @PreAuthorize("hasAuthority('file_version:restore')")
    @Operation(summary = "恢复版本", description = "恢复指定版本的文件")
    public ApiResponse<Void> restoreVersion(
            @Parameter(description = "版本ID", required = true) @PathVariable String id) {
        fileVersionService.restoreVersion(id);
        return ApiResponse.successVoid("恢复成功");
    }
}

